<?php
/**
 * UCClient框架
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCClient extends CComponent {
	
	/**
	 * uc_client基于项目根目录的路径
	 * @var string
	 */
	public $clientPath = 'interface/uc';
	
	/**
	 * 内置UC接口类
	 * @var array
	 */
	public static $classes = array(
		'user' => 'UCUser',
		'avatar' => 'UCAvatar',
	);
	
	/**
	 * 已实例化的接口
	 * @var array
	 */
	private static $_coms = array();
	
	public function init() {
		include Yii::app()->basePath.'/../'.$this->clientPath.'/config.inc.php';
		include Yii::app()->basePath.'/../'.$this->clientPath.'/client/client.php';
	}
	
	/**
	 * 获取UC接口
	 * @see CComponent::__get()
	 */
	public function __get($name) {
		if(isset(self::$classes[$name])) {
			if(!isset(self::$_coms[$name])) {
				self::$_coms[$name] = new self::$classes[$name]($this);
			}
			return self::$_coms[$name];
		} else {
			return parent::__get($name);
		}
	}
	
	/**
	 * 获取UC SERVER的地址
	 * @return string
	 */
	public function getUcUrl() {
		return UC_API;
	}
	
	
}

/**
 * UC接口
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCInterface extends CComponent {
	/**
	 * UC 框架实例
	 * @var UUClient
	 */
	protected $_client;
	
	public function __construct($client) {
		$this->_client = $client;
	}
	
	/**
	 * 获取Client框架实例
	 * @return UCClient
	 */
	public function getClient() {
		return $this->_client;
	}
	
}

/**
 * UC用户接口
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCUser extends UCInterface {
	
	/**
	 * 用户注册
	 * @param string $username
	 * @param string $password
	 * @param string $email
	 * @return UCRUserReg
	 */
	public function userRegister($username, $password, $email) {
		return new UCRUserReg(uc_user_register($username, $password, $email));
	}
	
	/**
	 * 用户登录
	 * @param string $username
	 * @param string $password
	 * @param string $isuid
	 * @return UCRUserLogin
	 */
	public function userLogin($username, $password, $isuid = false) {
		return new UCRUserLogin(uc_user_login($username, $password, $isuid));
	}
	
	/**
	 * 用户同步登录
	 * @param integer $uid
	 * @return UCRJs
	 */
	public function userSyncLogin($uid) {
		return new UCRJs(uc_user_synlogin($uid));
	}
	
	/**
	 * 用户同步退出
	 * @return UCRJs
	 */
	public function userSyncLogout() {
		return new UCRJs(uc_user_synlogout());
	}
	
	/**
	 * 用户删除
	 * @param integer $uid
	 * @return UCRBoolean
	 */
	public function userDelete($uid) {
		return new UCRBoolean(uc_user_delete($uid));
	}
	
	/**
	 * 用户编辑
	 * @param string $username
	 * @param string $oldpw
	 * @param string $newpw
	 * @param string $email
	 * @param boolean $ignoreoldpw = false
	 * @return UCRUserEdit
	 */
	public function userEdit($username, $oldpw, $newpw, $email = null, $ignoreoldpw = false) {
		return new UCRUserEdit(uc_user_edit($username, $oldpw, $newpw, $email, $ignoreoldpw));
	}
	
	/**
	 * 获取用户资料
	 * @param string $username
	 * @param boolean $isuid = false
	 * @return UCRGetUser
	 */
	public function getUser($username, $isuid = false) {
		return new UCRGetUser(uc_get_user($username, $isuid));
	}
	
}

/**
 * UC头像接口
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCAvatar extends UCInterface {
	
	const IMAGE_SIZE_BIG = 'big';
	const IMAGE_SIZE_MIDDLE = 'middle';
	const IMAGE_SIZE_SMALL = 'small';
	
	public function getUrl($uid, $size) {
		return $this->getClient()->getUcUrl()."/avatar.php?uid={$uid}&size={$size}";
	}
	
}

/**
 * UC请求应答
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCResponse extends CComponent {
	
	/**
	 * 请求原文 ...
	 * @var mixed
	 */
	protected $_rawContent;
	
	public function __construct($content) {
		$this->_rawContent = $content;
		$this->init();
	}
	
	protected function init() {
		
	}	
	
	/**
	 * 获取错误消息
	 * @return string
	 */
	public function getErrorMsg() {
		return '';
	}
	
	/**
	 * 是否错误的请求应答
	 * @return boolean
	 */
	public function getIsError() {
		return false;
	}
	
	/**
	 * 获取请求原文
	 * @return mixed
	 */
	public function getRawContent() {
		return $this->_rawContent;
	}
	
	public function __toString() {
		return $this->_rawContent;
	}
	
	public function toString() {
		return $this->_rawContent;
	}
}

/**
 * UC请求应答-逻辑型
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCRBoolean extends UCResponse {
	public function getIsError() {
		return $this->_rawContent ? true : false;
	}
}

/**
 * UC请求应答-JS型
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCRJs extends UCResponse {
	protected $_srcs = null;
	
	/**
	 * 注册JAVASCRIPT
	 * @return boolean
	 */
	public function register() {
		$result = true;
		foreach($this->getSrcs() as $one) {
			$result &= Yii::app()->clientScript->registerScriptFile(
				$one
			);
		}
		return $result;
	}

	/**
	 * 从原文中获取javascript src
	 * @return array
	 */
	public function getSrcs() {
		if(!$this->_srcs) {
			preg_match_all('/src="(.*?)"/s', $this->getRawContent(), $matches);
			foreach($matches[1] as $one) {
				$this->_srcs[] = $one;
			}
		}
		return $this->_srcs;
	}	
}

/**
 * UC请求应答-用户编辑
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCRUserEdit extends UCResponse {
	
	public function getIsError() {
		return $this->_rawContent > 0 ? true : false;
	}
	
	
	public function getErrorMsg() {
		if($this->_rawContent == -1) {
			return '旧密码不正确';
		} elseif($this->_rawContent == -4) {
			return 'Email 格式有误';
		} elseif($this->_rawContent == -5) {
			return 'Email 不允许注册';
		} elseif($this->_rawContent == -6) {
			return '该 Email 已经被注册';
		}
	}
}

/**
 * UC请求应答-获取用户信息
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCRGetUser extends UCResponse {
	
	public $uid;
	public $username;
	public $email;
		
	protected function init() {
		if($this->_rawContent)
			list($this->uid, $username, $email) = $this->_rawContent;
	}
	
	public function getIsError() {
		return $this->_rawContent ? true : false;
	}
	
	public function getErrorMsg() {
		if(!$this->_rawContent) {
			return '用户不存在';
		}
		return '';
	}
}

/**
 * UC请求应答-用户登录
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCRUserLogin extends UCResponse {
	
	public $uid;
	public $username;
	public $password;
	public $email;
	
	protected function init() {
		list($this->uid, $this->username, $this->password, $this->email) = $this->rawContent;	
	}
	
	public function getIsError() {
		return !($this->uid > 0);
	}
	
	public function getErrorMsg() {
		if($this->uid > 0) {
			return '登录成功';
		} elseif($this->uid == -1) {
			return '用户不存在,或者被删除';
		} elseif($this->uid == -2) {
			return '密码错';
		} else {
			return '未定义';
		}
	}
}

/**
 * UC请求应答-用户注册
 * @author yangdongqi <yangdongqi@hayzone.com>
 */
class UCRUserReg extends UCResponse {
	public function getIsError() {
		return !($this->_rawContent > 0);
	}
	
	public function getErrorMsg() {
		if($this->_rawContent <= 0) {
			if($this->_rawContent == -1) {
				return '用户名不合法';
			} elseif($this->_rawContent == -2) {
				return '包含要允许注册的词语';
			} elseif($this->_rawContent == -3) {
				return '用户名已经存在';
			} elseif($this->_rawContent == -4) {
				return 'Email 格式有误';
			} elseif($this->_rawContent == -5) {
				return 'Email 不允许注册';
			} elseif($this->_rawContent == -6) {
				return '该 Email 已经被注册';
			} else {
				return '未定义';
			}
		} else {
			return '注册成功';
		}
		
	}
}


?>